﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using kxcxw.Data.Entities;
using kxcxw.Data;
using Webdiyer.WebControls.Mvc;
using kxcxw.Data.Common;

namespace KXCXW.Web.Areas.Admin.Controllers
{
    public class InfoController : Controller
    {
        private KXCXWDBContext db = new KXCXWDBContext();
        private CommonRepositories comm = new CommonRepositories();
        //
        // GET: /Info/

        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Index(string sortOrder, string searchString, int? page = 1)
        {
            var publichinformation = from s in db.PublichInformations select s;
            string PromotionType = "";
            string AreaType = "";
            string InfoSource = "";
            if (searchString != null && searchString != "")
            {
                string[] searcharr = searchString.Split(';');
                if (!String.IsNullOrEmpty(searchString))
                {
                    foreach (string arr1 in searcharr)
                    {
                        string[] arr2 = arr1.Split(':');
                        string feidName = arr2[0];
                        string value = arr2[1];
                        if (feidName == "title")
                        {
                            publichinformation = publichinformation.Where(s => s.Title.ToUpper().Contains(value));
                            ViewBag.searchTitle = value;
                        }
                        else if (feidName == "pt" && value != "")
                        {
                            publichinformation = publichinformation.Where(s => s.PromotionType == value);
                            PromotionType = value;
                        }
                        else if (feidName == "at" && value != "")
                        {
                            publichinformation = publichinformation.Where(s => s.AreaType == value);
                            AreaType = value;
                        }
                        else if (feidName == "sl" && value != "")
                        {
                            publichinformation = publichinformation.Where(s => s.InfoSource == value);
                            InfoSource = value;
                        }
                    }
                }
            }
            var sl = comm.getTypeListByIdForDropDownList(DataConstants.PromotionTypeItemID);
            var s2 = comm.getTypeListByIdForDropDownList(DataConstants.SourceTypeItemID);
            var s5 = comm.getTypeListByIdForDropDownList(DataConstants.AreaTypeItemID);
            ViewBag.AreaType = new SelectList(s5, "Id", "Name", AreaType);
            ViewBag.PromotionTypeList = new SelectList(sl, "Id", "Name", PromotionType);
            ViewBag.SourceList = new SelectList(s2, "Id", "Name", InfoSource);
            switch (sortOrder)
            {
                case "Date":
                    publichinformation = publichinformation.OrderBy(s => s.PublishDate);
                    ViewBag.DateSortParm = "Date";
                    break;
                case "Date desc":
                    publichinformation = publichinformation.OrderByDescending(s => s.PublishDate);
                    ViewBag.DateSortParm = "Date desc";
                    break;
                case "VisitsNumber":
                    publichinformation = publichinformation.OrderBy(s => s.VisitsNumber);
                    ViewBag.DateSortParm = "VisitsNumber";
                    break;
                case "VisitsNumber desc":
                    publichinformation = publichinformation.OrderByDescending(s => s.VisitsNumber);
                    ViewBag.DateSortParm = "VisitsNumber desc";
                    break;
                default:
                    publichinformation = publichinformation.OrderByDescending(s => s.PublishDate);
                    break;
            }
            int pageSize = 15;
            int pageIndex = page ?? 1;
            PagedList<PublichInformation> temp = publichinformation.ToPagedList(pageIndex, pageSize);
            if (Request.IsAjaxRequest())
                return PartialView("List", temp);
            return View(temp);
        }

        //
        // GET: /Info/Details/5

        [Authorize(Roles = DataConstants.Admin)]
        public ViewResult Details(string id)
        {
            PublichInformation publichinformation = db.PublichInformations.Find(id);
            if (publichinformation.AreaType != null)
            {
                ViewData["areaType"] = (from q in db.SysItemDetials
                                        where q.ItemDeitalID == publichinformation.AreaType
                                        select q).FirstOrDefault().itemDetialName;
            }
            return View(publichinformation);
        }

        [Authorize(Roles = DataConstants.Admin)]
        public ViewResult CategoryMove()
        {
            var s3 = comm.getRootProductCategoryList();
            ViewBag.rootCategoryList = new SelectList(s3, "Id", "Name");
            List<SelectListItem> Items = new List<SelectListItem>();
            Items.Add(new SelectListItem { Value = "", Text = "请选择类别" });
            ViewBag.CategoryList = Items;
            return View();
        }

        [HttpPost]
        [Authorize(Roles = DataConstants.Admin)]
        public ViewResult CategoryMove(string ItemDeitalIDFrom, string ItemDeitalIDTo)
        {
            var PublichInformationlist = (from q in db.PublichInformations
                                          where q.ItemDeitalID == ItemDeitalIDFrom
                                          select q).ToList();
            int count = 0;
            foreach (PublichInformation p in PublichInformationlist)
            {
                p.ItemDeitalID = ItemDeitalIDTo;
                db.Entry(p).State = EntityState.Modified;
                count++;
            }
            db.SaveChanges();
            ViewBag.count = count;
            var s3 = comm.getRootProductCategoryList();
            ViewBag.rootCategoryList = new SelectList(s3, "Id", "Name", ItemDeitalIDFrom);
            var s4 = comm.getProductCategoryListById(ItemDeitalIDFrom);
            ViewBag.CategoryList = new SelectList(s4, "ItemDeitalID", "itemDetialName", ItemDeitalIDTo);
            return View();
        }

        public JsonResult QY(string ParentID)
        {
            var list = comm.getProductCategoryListById(ParentID);
            List<SelectListItem> Items = new List<SelectListItem>();
            Items.Add(new SelectListItem { Value = "", Text = "请选择类别" });
            foreach (SysItemDetial item in list)
            {
                int count = (from q in db.PublichInformations
                             where q.ItemDeitalID == item.ItemDeitalID
                             select q).ToList().Count();
                Items.Add(new SelectListItem { Value = item.ItemDeitalID, Text = item.itemDetialName + "(" + count + ")" });
            }
            return Json(Items, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /Info/Create
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Create()
        {
            ViewBag.PublishID = new SelectList(db.SysUserInfoes, "UserID", "UserName");
            ViewBag.AreaType = new SelectList(db.SysItemDetials, "ItemDeitalID", "itemDetialName");
            return View();
        }

        //
        // POST: /Info/Create

        [HttpPost]
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Create(PublichInformation publichinformation)
        {
            if (ModelState.IsValid)
            {
                db.PublichInformations.Add(publichinformation);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.PublishID = new SelectList(db.SysUserInfoes, "UserID", "UserName", publichinformation.PublishID);
            ViewBag.AreaType = new SelectList(db.SysItemDetials, "ItemDeitalID", "itemDetialName", publichinformation.AreaType);
            return View(publichinformation);
        }

        //
        // GET: /Info/Edit/5
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Edit(string id)
        {
            PublichInformation publichinformation = db.PublichInformations.Find(id);

            var sl = comm.getTypeListByIdForDropDownList(DataConstants.PromotionTypeItemID);
            var s2 = comm.getTypeListByIdForDropDownList(DataConstants.SourceTypeItemID);
            var s5 = comm.getTypeListByIdForDropDownList(DataConstants.AreaTypeItemID);
            ViewBag.AreaType = new SelectList(s5, "Id", "Name", publichinformation.AreaType);
            CommonRepositories cr = new CommonRepositories();
            PublichInformation pinfo = (from q in db.PublichInformations
                                        where q.InformationID == id
                                        select q).FirstOrDefault();

            ViewBag.PromotionTypeList = new SelectList(sl, "Id", "Name", pinfo.PromotionType);
            ViewBag.SourceList = new SelectList(s2, "Id", "Name", pinfo.InfoSource);

            var s3 = comm.getRootProductCategoryList();
            if (pinfo.ItemDeitalID != null)
            {
                SysItemDetial sid = (from q in db.SysItemDetials
                                     where q.ItemDeitalID == pinfo.ItemDeitalID
                                     select q).FirstOrDefault();
                var s4 = comm.getProductCategoryListById(sid.ItemDeitalPID);
                ViewBag.rootCategoryList = new SelectList(s3, "Id", "Name", sid.ItemDeitalPID);
                ViewBag.CategoryList = new SelectList(s4, "ItemDeitalID", "itemDetialName", sid.ItemDeitalID);
            }
            else
            {
                ViewBag.rootCategoryList = new SelectList(s3, "Id", "Name");
                List<SelectListItem> Items = new List<SelectListItem>();
                Items.Add(new SelectListItem { Value = "", Text = "请选择类别" });
                ViewBag.CategoryList = Items;
            }
            if (string.IsNullOrEmpty(publichinformation.Other))
                publichinformation.Other = "输入赠品名称";
            return View(publichinformation);
        }

        //
        // POST: /Info/Edit/5
        [Authorize(Roles = DataConstants.Admin)]
        [HttpPost]
        public ActionResult Edit(PublichInformation publichinformation)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.Entry(publichinformation).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                }
            }
            var sl = comm.getTypeListByIdForDropDownList(DataConstants.PromotionTypeItemID);
            var s2 = comm.getTypeListByIdForDropDownList(DataConstants.SourceTypeItemID);
            var s5 = comm.getTypeListByIdForDropDownList(DataConstants.AreaTypeItemID);
            ViewBag.AreaType = new SelectList(s5, "Id", "Name", publichinformation.AreaType);
            CommonRepositories cr = new CommonRepositories();
            PublichInformation pinfo = (from q in db.PublichInformations
                                        where q.InformationID == publichinformation.InformationID
                                        select q).FirstOrDefault();

            ViewBag.PromotionTypeList = new SelectList(sl, "Id", "Name", pinfo.PromotionType);
            ViewBag.SourceList = new SelectList(s2, "Id", "Name", pinfo.InfoSource);

            var s3 = comm.getRootProductCategoryList();
            if (pinfo.ItemDeitalID != null)
            {
                SysItemDetial sid = (from q in db.SysItemDetials
                                     where q.ItemDeitalID == pinfo.ItemDeitalID
                                     select q).FirstOrDefault();
                var s4 = comm.getProductCategoryListById(sid.ItemDeitalPID);
                ViewBag.rootCategoryList = new SelectList(s3, "Id", "Name", sid.ItemDeitalPID);
                ViewBag.CategoryList = new SelectList(s4, "ItemDeitalID", "itemDetialName", sid.ItemDeitalID);
            }
            else
            {
                ViewBag.rootCategoryList = new SelectList(s3, "Id", "Name");
                List<SelectListItem> Items = new List<SelectListItem>();
                Items.Add(new SelectListItem { Value = "", Text = "请选择类别" });
                ViewBag.CategoryList = Items;
            }
            return View(publichinformation);
        }

        //
        // GET: /Info/Delete/5
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Delete(string id, string sortOrder, string searchString, int page)
        {
            PublichInformation publichinformation = db.PublichInformations.Find(id);
            db.PublichInformations.Remove(publichinformation);
            db.SaveChanges();//string sortOrder, string searchString, int? page = 1
            return RedirectToAction("Index", new { sortOrder = sortOrder, searchString = searchString, page = page });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}